//
//  YGPatientLoginViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/14.
//

#import "YGPatientLoginViewController.h"
#import "YGLoginView.h"
#import "YGThirdLoginView.h"
#import "YGLoginBLL.h"
#import "YGRegisterViewController.h"
#import "UserInfoManager.h"
#import "YGRootView.h"
#import "YGForgetPwdViewController.h"
#import "YGFoodDetailViewController.h"

@interface YGPatientLoginViewController () <YGLoginViewDelegate>
@property (nonatomic, strong) UILabel  *tipsLabel;
@property (nonatomic, strong) YGLoginView *loginView;
@property (nonatomic, strong) UIButton *nurBtn;
@property (nonatomic, strong) UIButton *fpBtn;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) YGThirdLoginView *thirdLoginView;


@end

@implementation YGPatientLoginViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.navigationController.navigationBar.barTintColor=UIColorFromRGB(0x37C8D7);
    //设置navigation的返回按钮属性
    UIColor *color =UIColorFromRGB(0xFFFFFF);
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    self.navigationItem.leftBarButtonItem = nil;

    [self initView];
    [self initContraint];
    
    [self requestData];
    // Do any additional setup after loading the view.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    YGFoodDetailViewController *vc = [[YGFoodDetailViewController alloc] init];
    vc.foodId = @"1541219531339596881";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - public
#pragma mark - private
- (void)initView{
    [self.view addSubview:self.tipsLabel];
    [self.view addSubview:self.loginView];
    [self.view addSubview:self.nurBtn];
    [self.view addSubview:self.fpBtn];
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.thirdLoginView];
}

- (void)initContraint{
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(140);
        make.left.mas_equalTo(25);
    }];
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tipsLabel.mas_bottom).offset(23);
        make.left.mas_equalTo(self.tipsLabel.mas_left);
        make.height.mas_equalTo(210);
        make.right.mas_equalTo(self.view).offset(-25);
    }];
    
    
    [self.nurBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.loginView.mas_bottom).offset(15);
            make.left.mas_equalTo(self.loginView.mas_left);
            
    }];
    
    [self.fpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.loginView.mas_bottom).offset(15);
            make.right.mas_equalTo(self.loginView.mas_right);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fpBtn.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
    [self.thirdLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.logoImageView.mas_bottom).offset(25);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        
    }];
}

- (void)requestData{
    WeakSelf(self);
    [YGLoginBLL requestLogoWithSuccess:^(YGLogoModel * _Nonnull model) {
        [weakself.logoImageView sd_setImageWithURL:[NSURL URLWithString:model.logo]];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}
- (void)loginSuccess{
    
    [YGRootView resetRootViewController];
    
    
}

- (void)clickRegisterAction{
    YGRegisterViewController *vc = [[YGRegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)clickForgetPwdAction{
    YGForgetPwdViewController *vc2=[[YGForgetPwdViewController alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
    
    
    
}

#pragma mark - delegate
- (void)loginViewLoginWithPhone:(NSString *)phone pwd:(NSString *)pwd loginType:(LoginType)type{
    [self showWithStatus:@"登录中..."];
    WeakSelf(self);
    [YGLoginBLL requestLoginWithPhone:phone pwd:pwd success:^(YGUserInfoModel * _Nonnull model) {
        [[UserInfoManager shareInstance] configureUserInfo:model];
        [weakself loginSuccess];
        [weakself dismissHud];
    } fail:^(NSError * _Nonnull error) {
        [weakself dismissHud];

    }];
}
#pragma mark - setter

- (UILabel *)tipsLabel{
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.text = @"营养康复平台欢迎您";
        _tipsLabel.textColor = UIColorFromRGB(0x37C8D7);
        _tipsLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:21];
       
    }
    return _tipsLabel;
}

- (YGLoginView *)loginView{
    if (!_loginView) {
        _loginView = [[YGLoginView alloc] init];
        _loginView.delegate = self;
    }
    return _loginView;
}



- (UIButton *)nurBtn{
    if (!_nurBtn) {
        _nurBtn = [[UIButton alloc] init];
        [_nurBtn addTarget:self action:@selector(clickRegisterAction) forControlEvents:UIControlEventTouchUpInside];
        _nurBtn.titleLabel.font= [UIFont systemFontOfSize:14];
        [_nurBtn setTitle:@"新用户注册" forState:UIControlStateNormal];
        [_nurBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateNormal];
        [_nurBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateSelected];
    }
    return _nurBtn;
}

- (UIButton *)fpBtn{
    if (!_fpBtn) {
        _fpBtn = [[UIButton alloc] init];
        [_fpBtn addTarget:self action:@selector(clickForgetPwdAction) forControlEvents:UIControlEventTouchUpInside];
        _fpBtn.titleLabel.font= [UIFont systemFontOfSize:14];
        [_fpBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_fpBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        [_fpBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateSelected];
    }
    return _fpBtn;
}
- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        
    }
    return _logoImageView;
}
- (YGThirdLoginView *)thirdLoginView{
    if (!_thirdLoginView) {
        _thirdLoginView = [[YGThirdLoginView alloc] init];
        
    }
    return _thirdLoginView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
